<template>
  <u-popup :show="show" mode="center" @close="closeDialog" round>
    <view class="dialog-content">
      <view class="dialog-header">
        <text class="dialog-title">扫码数据编辑</text>
      </view>

      <view class="dialog-body" v-if="item">
        <view class="dialog-row">
          <text class="dialog-label">物料编码:</text>
          <text class="dialog-value">{{ item.cInvCode }}</text>
        </view>

        <view class="dialog-row">
          <text class="dialog-label">物料名称:</text>
          <text class="dialog-value">{{ item.cInvName }}</text>
        </view>

        <view class="dialog-row">
          <text class="dialog-label">采购订单:</text>
          <text class="dialog-value">{{ item.cPOID }}</text>
        </view>

        <view class="dialog-row">
          <text class="dialog-label">可收货数:</text>
          <text class="dialog-value">{{ item.availableQty }}</text>
        </view>

        <view class="dialog-row">
          <text class="dialog-label">到货数量:</text>
          <u--input
            v-model="item.revQty"
            type="number"
            placeholder="请输入到货数量"
            border="bottom"
            class="dialog-input"
          ></u--input>
        </view>

        <view class="dialog-row">
          <text class="dialog-label">仓库:</text>
          <u--input
            v-model="item.whCode"
            placeholder="请输入仓库编码"
            border="bottom"
            class="dialog-input"
          ></u--input>
        </view>

        <view class="dialog-row" v-if="item.isBatch">
          <text class="dialog-label">批次:</text>
          <u--input
            v-model="item.cBatch"
            placeholder="请输入批次"
            border="bottom"
            class="dialog-input"
          ></u--input>
        </view>

        <view class="dialog-row">
          <text class="dialog-label">单位:</text>
          <text class="dialog-value">{{ item.cComUnitName }}</text>
        </view>
      </view>

      <view class="dialog-footer">
        <u-button @click="closeDialog" class="dialog-cancel-btn">取消</u-button>
        <u-button @click="saveDialog" v-if="showSaveButton" type="primary" class="dialog-save-btn">保存</u-button>
      </view>
    </view>
  </u-popup>
</template>

<script>
import { saveArrivalVouchs } from "@/api/work/wareHourse/arrival.js";
import user from "@/store/modules/user";

export default {
  name: "ScanEditDialog",
  data() {
    return {
      showSaveButton: true,
    };
  },
  props: {
    show: {
      type: Boolean,
      default: false
    },
    item: {
      type: Object,
      default: () => null
    },
    arrivalList: {
      type: Array,
      default: () => []
    }
  },
  methods: {
    closeDialog() {
      this.$emit('closeDialog');
    },

    saveDialog() {
      this.showSaveButton = false;
      // 验证必要字段
      if (!this.item.revQty || parseFloat(this.item.revQty) <= 0) {
        this.$modal.msgError("到货数量必须大于0");
        this.showSaveButton = true;
        return;
      }

      if (!this.item.whCode) {
        this.$modal.msgError("仓库编码不能为空");
        this.showSaveButton = true;
        return;
      }

      if (this.item.isBatch) {
        if (!this.item.cBatch) {
          this.$modal.msgError("批次不能为空");
          this.showSaveButton = true;
          return;
        }
      }

      const revQty = parseFloat(this.item.revQty);
      const availableQty = parseFloat(this.item.availableQty);

      // 验证到货数量不能大于可到货数量
      if (revQty > availableQty) {
        this.$modal.msgError("到货数量不能大于可收货数量");
        this.showSaveButton = true;
        return;
      }

      // 遍历arrivalList数据，查找是否存在供应商名字一样的到货单
      let matchedArrival = null;
      if (this.arrivalList && this.arrivalList.length > 0) {
        matchedArrival = this.arrivalList.find(arrival =>
          arrival.vName === this.item.vName
        );
      }
      // 构建提交数据
      const submitData = {
        // 子表信息
        iPOsID: this.item.iPOsID,
        revQty: parseFloat(this.item.revQty),
        cBatch: this.item.cBatch,
        whCode: this.item.whCode,
        cPOID: this.item.cPOID,
        cInvCode: this.item.cInvCode,
        isBatch: this.item.isBatch,
        makeMan: user.state.nickName
      };

      console.log("提交数据：", submitData);

      // 如果找到匹配的到货单，将该单据的ID和单据号添加到提交数据中
      if (matchedArrival) {
        submitData.id = matchedArrival.id;  // 主表ID
        submitData.cCode = matchedArrival.cCode;  // 单据号
      }
      // 调用saveArrivalVouchs方法保存数据
      saveArrivalVouchs(submitData).then(response => {
        if (response.code === 200) {
          if (response.data.fatherId) {
            // 检查arrivalList中是否已存在相同的vName
            const exists = this.arrivalList.some(item => item.vName === this.item.vName);
            if (!exists) {
              let arrivalData = {
                id: response.data.fatherId,
                cCode: response.data.newCode,
                vName: this.item.vName
              };
              this.arrivalList.push(arrivalData);
            }
          }
          this.$modal.msgSuccess("保存成功");
          // 保存成功后，通知父组件移除当前扫描项并更新到货单数据
          this.$emit('saveDialog', this.item);
          // 新增事件，将更新后的arrivalList传递给父组件
          this.$emit('updateArrivalList', this.arrivalList);
        } else {
          this.$modal.msgError("保存失败：" + (response.msg || ""));
        }
        this.showSaveButton = true;
      }).catch(error => {
        this.$modal.msgError("保存失败：" + error.message);
        this.showSaveButton = true;
      });
    }
  }
};
</script>

<style scoped>
/* 添加弹窗样式 */
.dialog-content {
  width: 600rpx;
  background-color: #fff;
  border-radius: 12rpx;
  padding: 30rpx;
}

.dialog-header {
  text-align: center;
  margin-bottom: 30rpx;
}

.dialog-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.dialog-row {
  display: flex;
  align-items: center;
  margin-bottom: 30rpx;
}

.dialog-label {
  width: 160rpx;
  font-size: 28rpx;
  color: #666;
}

.dialog-value {
  flex: 1;
  font-size: 28rpx;
  color: #333;
}

.dialog-input {
  flex: 1;
}

.dialog-footer {
  display: flex;
  justify-content: space-between;
  margin-top: 20rpx;
}

.dialog-cancel-btn {
  flex: 1;
  margin-right: 20rpx;
}

.dialog-save-btn {
  flex: 1;
  margin-left: 20rpx;
}
</style>
